// login_2.js
const app = getApp(); // 获取全局应用实例
const baseURL = app.globalData.baseURL
Page({
  getPhoneNumber(e) {
    const { encryptedData, iv } = e.detail
    if (!encryptedData) {
      wx.showToast({ title: '登录已取消', icon: 'none' })
      return
    }

    wx.login({
      success: (res) => {
        wx.request({
          url:  `${baseURL}api/login_2/`,
          method: 'POST',
           data:{
            code: res.code,
            encryptedData,
            iv
          },
          success: (res) => {
            if (res.data.success) {
              wx.setStorageSync('token', res.data.token)
              wx.setStorageSync('mobile', res.data.mobile)
              // 返回原页面
              const pages = getCurrentPages()
              if(pages.length >= 2) {
                wx.navigateBack()
              } else {
                wx.switchTab({ url: '/pages/index/index' })
              }
            }
          }
        })
      }
    })
  }
})
